package com.wangpu.oa.web.servlet.userhome;

import java.io.IOException;
import java.io.Writer;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.wangpu.oa.ao.QueryUserAo;
import com.wangpu.oa.dto.JSONResponse;
import com.wangpu.oa.dto.QueryResult;
import com.wangpu.oa.dto.UserContext;
import com.wangpu.oa.exception.OAException;
import com.wangpu.oa.service.Systemservice;
import com.wangpu.oa.service.impl.SystemServiceImpl;
import com.wangpu.oa.util.JSONUtil;
@WebServlet("/api/user/loadMenu")
public class LoadMyMenusServlet extends HttpServlet{
	@Override
	protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		JSONResponse result=new JSONResponse();
		try {
			// 1 获取参数(清理和验证参数)
			String key=req.getParameter("key");
			String flag=req.getParameter("state");
			String pageNo=req.getParameter("pageNo");
			String pageSize=req.getParameter("pageSize");
			
			// 2 调用业务方法(进行转换)
			QueryUserAo ao=new QueryUserAo();
			
			ao.setKey(key);
			ao.setFlag(flag);
			ao.setPageNo(pageNo);
			ao.setPageSize(pageSize);
			
			Systemservice service=new SystemServiceImpl();
			
			HttpSession session=req.getSession();
			
			UserContext uc=(UserContext) session.getAttribute("CURRENT-USER");
			QueryResult queryResult=service.queryUser(ao,uc.getId());
			// 3 返回结果
			
			result.setCode("ok");
			result.setData(queryResult);
		} catch (OAException e) {
			result.setCode("001");
			result.setMessage(e.getMessage());
		} catch (Exception e){
			result.setCode("error");
			result.setMessage("系统繁忙，请稍候再试!");
			//打印异常信息，用于调试
			e.printStackTrace();
		}finally {
			
			Writer out=resp.getWriter();
			out.write(JSONUtil.toJsonString(result));
			out.close();
		}
	}
}
